package com.hzcx.app.simplechat.base;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;


import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.ui.publicui.PublicTipsDialog;
import com.hzcx.app.simplechat.util.toast.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;

public abstract class BaseDialog<T extends BasePresenter> extends Dialog implements IView {

    public static final String TAG=BaseActivity.class.getSimpleName();

    protected T mPresenter;

    private BaseLoadingDialog loadingDialog;

    public Context context;

    public BaseDialog(@NonNull Context context) {
        super(context, R.style.dialog_style);
        this.context=context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);

        initPresenter();
        initView();
        initData();

        if (mPresenter != null) {
            mPresenter.attachView(this);
        }


        setOnDismissListener(dialogInterface -> {
            if (mPresenter != null) {
                mPresenter.detachView();
            }
        });

//        EventBus.getDefault().register(this);
//
//        this.setOnDismissListener(new OnDismissListener() {
//            @Override
//            public void onDismiss(DialogInterface dialogInterface) {
//                EventBus.getDefault().unregister(this);
//            }
//        });
    }

    public abstract int getLayout();

    public abstract void initPresenter();

    public abstract void initView();

    public abstract void initData();

    @Override
    public void showError(String msg) {
        ToastUtils.show(msg);
    }

    @Override
    public void showLoading() {
        try {
            loadingDialog=new BaseLoadingDialog(context);
            loadingDialog.show();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void hideLoading() {
        try {
            if(loadingDialog!=null&&loadingDialog.isShowing()){
                loadingDialog.dismiss();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void dataEmpty() {

    }

    @Override
    public void showTips(String title, String content, PublicTipsDialog.OnPublicTipsClickListener listener) {

    }


}
